<template>
  <el-dialog
    :title="(!dataForm.id ? '新增': !disabled ? '修改' : '查看')"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             inline label-width="150px">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="dataForm.name" :disabled="disabled" placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item label="身份证号" prop="idCard">
        <el-input v-model="dataForm.idCard" :disabled="disabled" placeholder="身份证号(后6位)"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="dataForm.phone" :disabled="disabled" placeholder="手机号"></el-input>
      </el-form-item>
      <el-form-item label="卡号/卡种类型" prop="acctNo">
        <el-input v-model="dataForm.acctNo" :disabled="disabled" placeholder="卡号"></el-input>
      </el-form-item>
      <el-form-item label="分档等级" prop="level">
        <el-input-number v-model="dataForm.level" :min="1" :max="10" :disabled="disabled"
                         label="分档等级"></el-input-number>
      </el-form-item>
      <el-form-item label="次数" prop="number">
        <el-input v-model="dataForm.number" :disabled="disabled" placeholder="次数"></el-input>
      </el-form-item>
      <el-form-item label="白名单类型" prop="type">
        <el-select v-model="dataForm.type" :disabled="disabled" filterable placeholder="白名单类型">
          <el-option
            v-for="type in whitelistType"
            :key="type.value"
            :label="type.label"
            :value="type.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="过期时间" prop="lapseTime">
        <el-date-picker
          v-model="dataForm.lapseTime"
          :disabled="disabled"
          placeholder="选择日期"
          type="date"
          value-format="yyyy-MM-dd HH:mm:ss">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="是否有效" prop="valid">
        <el-dict code="IS_NOT" :disabled="disabled" v-model="dataForm.valid"></el-dict>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button v-if="!disabled" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>

export default {
  data() {
    return {
      disabled: false,
      visible: false,
      dataForm: {
        id: null,
        name: null,
        idCard: null,
        phone: null,
        acctNo: null,
        lapseTime: null,
        number: 1,
        level: null,
        valid: '1',
        type: null
      },
      dataRule: {
        phone: [
          {required: true, message: '手机号不能为空', trigger: 'blur'}
        ],
        name: [
          {required: true, message: '名称不能为空', trigger: 'blur'}
        ],
        number: [
          {required: true, message: '次数不能为空', trigger: 'blur'}
        ],
        lapseTime: [
          {required: true, message: '过期时间不能为空', trigger: 'change'}
        ],
        type: [
          {required: true, message: '白名单类型不能为空', trigger: 'change'}
        ],
      },
      whitelistType: []
    }
  },
  methods: {
    init(id, disabled, whitelistType) {
      this.whitelistType = whitelistType
      this.disabled = disabled
      this.dataForm.id = id || ''
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: `/mall/whitelist/info/${this.dataForm.id}`,
            method: 'get'
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm = data.whitelist
              this.dataForm.type = this.dataForm.type + ''
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm']
        .validate((valid) => {
          if (valid) {
            this.$http({
              url: `/mall/whitelist/${!this.dataForm.id ? 'save' : 'update'}`,
              method: 'post',
              data: this.dataForm
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500
                })
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          }
        })
    }
  }
}
</script>
